﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using GreatB2C.Config.SiteInfo;
using GreatB2C.Utility;

namespace GreatB2C.Web.Manage.SystemSet.SiteInfo
{
    public partial class UserParams : Inherit.ManagePage
    {
        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                LoadSetting( );
            }
        }

        protected void LoadSetting( )
        {
            radlEnableUserReg.SelectedValue = UserConfig.EnableUserReg.ToString( );
            txtProtocol.Value = StringHelper.XmlDecode( UserConfig.Protocol.ToString( ) );
            radlEnableMultiRegPerEmail.SelectedValue = UserConfig.EnableMultiRegPerEmail.ToString( );
            txtUserNameLimit.Text = UserConfig.UserNameLimit.ToString( );
            txtUserNameMax.Text = UserConfig.UserNameMax.ToString( );
            txtUserName_RegDisabled.Text = StringHelper.XmlDecode( UserConfig.UserName_RegDisabled.ToString( ) );
            HdnRegFields_MustFill.Value = UserConfig.RegFields_MustFill.ToString( );
            string[] must = UserConfig.RegFields_MustFill.Split( ',' );
            for ( int i=0 ; i < must.Length ; i++ )
            {
                for ( int j=0 ; j < LitRegFields.Items.Count ; j++ )
                {
                    if ( must[ i ] == LitRegFields.Items[ j ].Value )
                    {
                        LitRegFields_MustFill.Items.Add( LitRegFields.Items[ j ] );
                        LitRegFields.Items.RemoveAt( j );
                        break;
                    }
                }
            }
            HdnRegFields_SelectFill.Value = UserConfig.RegFields_SelectFill.ToString( );
            must = UserConfig.RegFields_SelectFill.Split( ',' );
            for ( int i=0 ; i < must.Length ; i++ )
            {
                for ( int j=0 ; j < LitRegFields.Items.Count ; j++ )
                {
                    if ( must[ i ] == LitRegFields.Items[ j ].Value )
                    {
                        LitRegFields_SelectFill.Items.Add( LitRegFields.Items[ j ] );
                        LitRegFields.Items.RemoveAt( j );
                        break;
                    }
                }
            }
            radlEnableRegCompany.SelectedValue = UserConfig.EnableRegCompany.ToString( );
            radlEnableCheckCodeOfReg.SelectedValue = UserConfig.EnableCheckCodeOfReg.ToString( );
            radlEnableQAofReg.SelectedValue = UserConfig.EnableQAofReg.ToString( );
            txtRegQuestion1.Text = UserConfig.RegQuestion1.ToString( );
            txtRegAnswer1.Text = UserConfig.RegAnswer1.ToString( );
            txtRegQuestion2.Text = UserConfig.RegQuestion2.ToString();
            txtRegAnswer2.Text = UserConfig.RegAnswer2.ToString();
            txtRegQuestion3.Text = UserConfig.RegQuestion3.ToString( );
            txtRegAnswer3.Text = UserConfig.RegAnswer3.ToString( );
            radlAdminCheckReg.SelectedValue = UserConfig.AdminCheckReg.ToString( );
            radlEmailCheckReg.SelectedValue = UserConfig.EmailCheckReg.ToString( );
            txtEmailOfRegCheck.Text = StringHelper.XmlDecode( UserConfig.EmailOfRegCheck.ToString( ) );
            ddlUserGroup.SelectedValue = UserConfig.UserGroup.ToString( );
            txtPresentExp.Text = UserConfig.PresentExp.ToString( );
            txtPresentMoney.Text = UserConfig.PresentMoney.ToString( );
            txtPresentPoint.Text = UserConfig.PresentPoint.ToString( );
            txtPresentValidNum.Text = UserConfig.PresentValidNum.ToString( );
            radlGetPasswordType.SelectedValue = UserConfig.GetPasswordType.ToString( );
            radlEnableCheckCodeOfLogin.SelectedValue = UserConfig.EnableCheckCodeOfLogin.ToString( );
            radlEnableMultiLogin.Text = UserConfig.EnableMultiLogin.ToString( );
            txtPresentExpPerLogin.Text = UserConfig.PresentExpPerLogin.ToString ( );
            txtAddPointPerLogin.Text = UserConfig.AddPointPerLogin.ToString( );
            txtMoneyExchangePoint.Text = UserConfig.MoneyExchangePoint.ToString( );
            txtMoneyExchangeValidDay.Text = UserConfig.MoneyExchangeValidDay.ToString( );
            txtUserExpExchangePoint.Text = UserConfig.UserExpExchangePoint.ToString( );
            txtUserExpExchangeValidDay.Text = UserConfig.UserExpExchangeValidDay.ToString( );
            txtPointName.Text = UserConfig.PointName.ToString( );
            txtPointUnit.Text = UserConfig.PointUnit.ToString( );
        }

        protected void BtnSubmit_Click( object sender , EventArgs e )
        {
            bool isSuccess = UserConfig.Config( radlEnableUserReg.SelectedValue.ToString( ) , StringHelper.XmlEncode( txtProtocol.Value.ToString( ).Trim( ) ) , radlEnableMultiRegPerEmail.SelectedValue.ToString( ) , txtUserNameLimit.Text.Trim( )
            , txtUserNameMax.Text.Trim( ) , StringHelper.XmlEncode( txtUserName_RegDisabled.Text.Trim( ) ) , HdnRegFields_MustFill.Value.Trim( ) , HdnRegFields_SelectFill.Value.Trim( ) , radlEnableRegCompany.SelectedValue.ToString( )
            , radlEnableCheckCodeOfReg.SelectedValue.ToString( ) , radlEnableQAofReg.SelectedValue.ToString( ) , txtRegQuestion1.Text.Trim( ) , txtRegAnswer1.Text.Trim( )
            , txtRegQuestion2.Text.Trim( ) , txtRegAnswer2.Text.Trim( ) , txtRegQuestion3.Text.Trim( ) , txtRegAnswer3.Text.Trim( )
            , radlAdminCheckReg.SelectedValue.ToString( ) , radlEmailCheckReg.SelectedValue.ToString( ) , StringHelper.XmlEncode( txtEmailOfRegCheck.Text.Trim( ) ) , ddlUserGroup.SelectedValue.ToString( )
            , txtPresentExp.Text.Trim( ) , txtPresentMoney.Text.Trim( ) , txtPresentPoint.Text.Trim( ) , txtPresentValidNum.Text.Trim( )
            , radlGetPasswordType.SelectedValue.ToString( ) , radlEnableCheckCodeOfLogin.SelectedValue.ToString( ) , radlEnableMultiLogin.Text.Trim( ) , txtPresentExpPerLogin.Text.Trim( )
            , txtAddPointPerLogin.Text.Trim( ) , txtMoneyExchangePoint.Text.Trim( ) , txtMoneyExchangeValidDay.Text.Trim( ) , txtUserExpExchangePoint.Text.Trim( )
            , txtUserExpExchangeValidDay.Text.Trim( ) , txtPointName.Text.Trim( ) , txtPointUnit.Text.Trim( ) );

            if ( isSuccess )
            {
                Response.Redirect( "../../Message.aspx?msg=" + Server.UrlEncode( "用户参数配置成功！" ) + "&rtn=SystemSet/SiteInfo/UserParams.aspx" );
            }
            else
            {
                Response.Redirect( "../../Message.aspx?msg=" + Server.UrlEncode( "用户参数配置失败，请联系管理员！" ) + "&rtn=SystemSet/SiteInfo/UserParams.aspx" );
            }
        }

        public string getPointName( )
        {
            if ( UserConfig.PointName.Trim( ) != "" )
                return UserConfig.PointName;
            else
                return "点券";
        }

        public string getPointUnit( )
        {
            if ( UserConfig.PointUnit.Trim( ) != "" )
                return UserConfig.PointUnit;
            else
                return "点";
        }
    }
}
